<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
	<title>客户管理</title>
	<%@ include file="/common/meta.jsp" %>
    <%@ include file="/common/import.jsp" %>
    
		<script>
		var orgId='${org.orgId}';
		$(function(){
			$('#userList').datagrid({
				title:'客户信息列表',
				iconCls:'icon-ok',
				fitColumns:true,
				nowrap:false,
				singleSelect:true,
				rownumbers:true,
				animate:true,
				collapsible:true,
				pagination:true,
				pageSize:10,
				pageList:[10,20,30],
				url:'${ctx}/crm/list!data.action',
				idField:'customerId',
				columns:[[
				     {field:'customerNo',title:'客户编号',width:60},
	                 {field:'customerName',title:'客户名称',width:140},			 
		             {field:'createTime',title:'创建时间',width:100},
		             {field:'customerType',title:'类型',width:100,
		                  formatter:function(value){
							if('A11001'==value){
								return '法人(其它组织)';
							}else if('A11002'==value){
								return '自然人';
							}else{
							    return '';
							 }
		                   }
		                 },
		            {field:'uctUserename',title:'登录账号',width:100},
		            {field:'auditState',title:'状态',width:100,
		                  formatter:function(value){
							if('1'==value){
								return '已激活';
							}else if('0'==value){
								return "<font color='red'>未激活</font>";
							}else{
							    return "<font color='red'>未激活</font>";
							 }
		                   }
		                 },
		            {field:'customerId',title:'操作',width:100,
		                  formatter:function(value,rowData,rowIndex){
							var val = "";
							if("1" == rowData.auditState){
								val = "<a href='#' onclick='printfile(\"" +value+ "\")'>打印</a>";
// 								val = "<a href='#' onclick='toPrint(\"" +value+ "\")'>打印</a>";
							}else if("0" == rowData.auditState || "" == rowData.auditState || null == rowData.auditState){
								val = "<a href='#' onclick='audit(\"" +value+ "\")'>激活</a>";
							}
		                	return val;
		                 }
		                }
				]],
				toolbar:[					
		          {
		              	text:'增加',
			              iconCls:'icon-add',
			              handler:function(){save();}
		          }
		          ,'-',
		          {
		              text:'删除',
		              iconCls:'icon-cut',
		              handler:function(){del();}
		          }
		            ,'-',
		          {
		              text:'修改',
		              iconCls:'icon-edit',
		              handler:function(){edit();}
		          }
		          /*	,'-',
		          {
		              text:'子账号管理',
		              iconCls:'icon-edit',
		              handler:function(){subEdit();}
		          }*/
		          	,'-',
		          {
		              text:'查看客户类型',
		              iconCls:'icon-search',
		              handler:function(){crmType();}
		          }
		          ,'-',
		          {
		              text:'刷新',
		              iconCls:'icon-reload',
		              handler:function(){
		                  reload();
		             }
		          }
		        ]
			});
		});	

	
		//查询
		 function searchList(){			
	    	var queryParams = $('#userList').datagrid('options').queryParams;	    
	       	var opt = $('#userList').datagrid('options'); 
	        $('#userList').datagrid('options').pageNumber=1;
	        queryParams["filter_customerName"] = $('#customerNameStr').val();
	        queryParams["filter_customerType"] = $('#customerTypeStr').val();
	        $('#userList').datagrid('reload');
	        var page = $('#userList').datagrid('getPager');
			page.pagination({pageNumber:1,pageSize:opt.pageSize});
		 }
		 //清除查询
		 function clearSearch(){					
		    $('#customerNameStr').val('');
		    $('#customerTypeStr').val('');
		 }
		
		//保存
		function save(){
			$('#userAddFrame').html('');
			var url = '${ctx}/crm/add.action';
			$('#userAddFrame').attr("src",url);
			$('#userAddWin').window('setTitle',"添加客户信息");	
			$('#userAddWin').window('open');				
		}
		
		//修改
		function edit(){
		    var obj = getSelected();
			if(obj){
				$('#userAddFrame').html('');
				var url = '${ctx}/crm/edit.action?id='+obj.customerId+'&dd='+new Date().getTime();
				$('#userAddWin').window('setTitle',"修改客户信息");	
				$('#userAddFrame').attr("src",url);
				$('#userAddWin').window('open');	
			}
		}
		
		//子账号管理
		function subEdit(){
		    var obj = getSelected();
			if(obj){
				$('#userAddFrame').html('');
				var url = '${ctx}/crm/user/index.action?mainUserId='+obj.customerId+'&dd='+new Date().getTime();
				$('#userAddWin').window('setTitle',"子账号管理");	
				$('#userAddFrame').attr("src",url);
				$('#userAddWin').window('open');	
			}
		}
		
		//子账号管理
		function crmType(){
		    var obj = getSelected();
			if(obj){
				$('#userAddFrame').html('');
				var url = '${ctx}/crm/customerTypeList.action?customerId='+obj.customerId+'&dd='+new Date().getTime();
				$('#userAddWin').window('setTitle',"查看客户类型");	
				$('#userAddFrame').attr("src",url);
				$('#userAddWin').window('open');	
			}
		}
		
		//重新加载数据
		function reload(){
			$('#userList').datagrid('reload');
		}
		
		function toPrint(id){
			alert("等待交易所提供模板再做。。");
		}
		
          //删除方法
		function del(){
			var obj = getSelected();
			if(obj){
				$.messager.confirm('确认','确定要删除: <font color=red><b>'+obj.customerName+'</b></font> ?',function(flag){  
			        if(flag){  
			            $.ajax({
						  url: '${ctx}/crm/del.action',
						  type: 'POST',
						  cache: false,
						  data: 'id='+obj.customerId,
						  success: function(data){
						  	var oTxt = jQuery.parseJSON(data);
						    var mess = oTxt.msg;	
							var flag = oTxt.flag;
					        if(flag=="T"){
								$.messager.alert('提示',mess, 'info',function(){
								   reload();
								});
							}else{
								$.messager.alert('提示',mess, 'error');
							}
						  }
						});
			        }  
			     });
			  }
		  }
		  
		//审核方法
		function audit(customerId){
			//$.messager.confirm('确认','确定要审核吗?',function(flag){  
			//   if(flag){  
			   $('#userAddFrame').html('');
				var url = '${ctx}/crm/audit.action?id='+customerId+'&dd='+new Date().getTime();
				$('#userAddWin').window('setTitle',"激活客户");	
				$('#userAddFrame').attr("src",url);
				$('#userAddWin').window('open');	 
			 //  } 
			//});
		  }
		  
		  //取得数据
		  function getSelected(){
		  	var rec = $('#userList').datagrid('getSelected');
		  	if(rec){
		  		return rec;
		  	}else{
		  		$.messager.alert('提示',"请选择需要操作记录!", 'info');
		  		return null;
		  	}
		  }
		  
		   //关闭win
		 function closeWin(){
		 	$('#userAddWin').window('close');	
		 }
		 
		 function printfile(customerId) {			
		 	var url ='${ctx}/crm/print.action?customerId='+customerId+"&T="+new Date().getTime();
		 	window.open(url);  
		}
		
	     
	</script>
</head>
<body>
<div title="查询"   class="easyui-panel" icon="icon-search"  collapsible="true">
		<table class="datagrid-body" width="100%">
			<tr>
				<td class="datagrid-header">客户名称</td>
				<td width="18px;"><input type="text" class="easyui-validatebox" id="customerNameStr"  name="customerNameStr" size="30" value=""/></td>
				<td class="datagrid-header">客户类型</td>
				<td width="18px;">
				<select name="customerTypeStr" id="customerTypeStr"  readonly="true" editable="false">
		                <option value="">--请选择--</option>
						<option value="A11001">
							法人
						</option>
						<option value="A11002">
							自然人
						</option>
						</select>
				</td>
				<td><a class="easyui-linkbutton" iconCls="icon-search" onclick="searchList()">查 询</a>
				<a class="easyui-linkbutton" iconCls="" href="javascript:void(0)" onclick="clearSearch();">清空</a>
				</td>
			</tr>
		</table>
		</div>
	<table id="userList"></table>
	<div id="userAddWin" class="easyui-window" title="客户信息" style="padding:5px;width:900px;height:450px; overflow: hidden;" align="center"
    	iconCls="icon-edit" closed="true" maximizable="false" minimizable="false" collapsible="false">
   	 	<iframe frameborder="0"  id="userAddFrame" height="100%" width="100%" scrolling="Yes" frameborder="0" ></iframe>
    </div>

</body>
</html>